module Ssp
  class ReportsController < ApplicationController

    def index
      @reports = report_scope.all

      @search_params = params[:search]
      if @search_params
        @reports = @reports.joins(:user)

        @nickname_or_ssid = @search_params[:nickname_or_ssid]
        @reports = @reports.where('ssp_users.nickname LIKE ? or ssp_users.ssid LIKE ?', "%#{@nickname_or_ssid}%", "%#{@nickname_or_ssid}%") if @nickname_or_ssid.present?

        @sstype = @search_params[:sstype]
        @reports = @reports.where(ssp_users: { sstype: @sstype }) if @sstype.present?

        @status = @search_params[:status]
        @reports = @reports.where(status: @status) if @status.present?

        @created_at_from = @search_params[:created_at_from]
        if @created_at_from.present?
          @reports = @reports.where('ssp_reports.created_at >= ?', Time.zone.parse(@created_at_from))
        end

        @created_at_to = @search_params[:created_at_to]
        if @created_at_to.present?
          @reports = @reports.where('ssp_reports.created_at < ?', Time.zone.parse(@created_at_to) + 1.day)
        end

        @publish_at_from = @search_params[:publish_at_from]
        if @publish_at_from.present?
          @reports = @reports.where('ssp_reports.publish_at >= ?', Time.zone.parse(@publish_at_from))
        end

        @publish_at_to = @search_params[:publish_at_to]
        if @publish_at_to.present?
          @reports = @reports.where('ssp_reports.publish_at < ?', Time.zone.parse(@publish_at_to) + 1.day)
        end
      else
        @reports = @reports.where('ssp_reports.created_at >= ?', Time.zone.now - 1.year)
      end

      respond_to do |format|
        format.html
        format.xlsx
      end
    end

    def show
      @report = report_scope.find(params[:id])
    end

    def new
      @report = report_scope.new
    end

    def create
      @report = report_scope.new(report_params)

      if @report.save
        redirect_to ssp_reports_path
      else
        render :new
      end
    end

    def edit
      @report = report_scope.find(params[:id])
    end

    def destroy
      @report = report_scope.find(params[:id])
      @report.update_attributes(hidden: true)
      redirect_to request.referer
    end

    def update
      @report = report_scope.find(params[:id])
      if @report.update_attributes(report_params)
        redirect_to ssp_reports_path
      else
        render :edit
      end
    end

    def confirm
      @report = report_scope.find(params[:id])
      @report.update_attributes(status: 'confirmed')
      redirect_to request.referer
    end

    def reject
      @report = report_scope.find(params[:id])
      @report.update_attributes(status: 'unconfirmed')
      redirect_to request.referer
    end

    def batch_confirm
      @reports = report_scope.where(id: params[:ids].split(','), status: 'unconfirmed')
      @reports.update_all(status: 'confirmed')
      redirect_to request.referer
    end

    def batch_reject
      @reports = report_scope.where(id: params[:ids].split(','), status: 'confirmed')
      @reports.update_all(status: 'unconfirmed')
      redirect_to request.referer
    end

    def batch_destroy
      @reports = report_scope.where(id: params[:ids].split(','))
      @reports.update_all(hidden: true)
      redirect_to request.referer
    end

    private

    def report_params
      params.require(:ssp_report).permit(:user_id, :publish_at, report_items_attributes: [ :id, :_destroy, :commodity_type, :commodity_id, :price, :count ])
    end

    def report_scope
      scope = if current_user.admin
          Report.all
        else
          Report.where(user_id: current_user)
        end
      scope = scope.where.not(hidden: true)

      scope = if params[:status]
          scope.where(status: params[:status])
        else
          scope
        end

      scope
    end
  end
end
